package cn.com.topchoice.mobile.library;

import android.app.Application;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;

public class BaseApplication extends Application {

	public static String PACKAGE_NAME;
	
	protected static BaseApplication mInstance;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		mInstance = this;
		
		// init the package name
		initPackageName();
	}

	/**
	 * Get the application instance
	 * 
	 * @return
	 */
	public static BaseApplication getInstance() {
		return mInstance;
	}
	
	private void initPackageName() {
		PACKAGE_NAME = getPackageName();
	}

    public String getVersion() {
    	// Get a version string for the app
    	StringBuilder sbVersion = new StringBuilder();
    	sbVersion.append(PACKAGE_NAME);
    	sbVersion.append(":");
    	try {
    		sbVersion.append(getPackageManager().getPackageInfo(getPackageName(), 0).versionCode);
    	} catch (NameNotFoundException e) {
    		throw new RuntimeException(e);
    	}
    	return sbVersion.toString();
    }
    
    /**
     * Check if it is first time to enter the current version app
     * 
     * @return
     */
    public boolean isFirstEnterApp() {
    	String ver = CommonPreferences.getInstance(this).getFirstEnterAPPVer();
    	if (TextUtils.isEmpty(ver) ||
    			!ver.equals(getVersion()))
    		return true;
    	else 
    		return false;
    }
    
    /**
     * Get the uuid of the device
     * 
     * @return
     */
    public String getDeviceUUIDString() {
    	String deviceId = CommonPreferences.getInstance(this).getDeviceUUID();
    	if (TextUtils.isEmpty(deviceId)) {
    		deviceId = UUID.getDeviceUUID(this);
    		CommonPreferences.getInstance(this).setDeviceUUID(deviceId);
    	}
    	return deviceId;
    }
}